<template>
  <el-container>
    <my-theme-card>
      <el-header>
        <el-row class="header-row">
          名称:<el-input v-model="conditions[0].value" style="width:150px;" placeholder="请输入内容" clearable />
          <el-button class="table-header-btn" type="primary" icon="el-icon-search" @click="search">查询</el-button>
        </el-row>
      </el-header>
      <el-main>
        <el-col :span="1"><div class="grid-content bg-purple-light">
          <el-button class="table-header-btn" type="primary" icon="el-icon-plus" size="mini" @click="addClick">新增</el-button>
        </div></el-col>
        <template>
          <el-table :data="weaponList" stripe style="width: 100%">
            <el-table-column type="expand">
              <template slot-scope="props">
                <el-form label-position="left" inline class="demo-table-expand">
                  <el-form-item label="名称">
                    <span>{{ props.row.name }}</span>
                  </el-form-item>
                  <el-form-item label="编号">
                    <span>{{ props.row.number }}</span>
                  </el-form-item>
                  <el-form-item label="星级">
                    <span>{{ props.row.star }}</span>
                  </el-form-item>
                  <el-form-item label="等级">
                    <span>{{ props.row.level }}</span>
                  </el-form-item>
                  <el-form-item label="图鉴类别">
                    <span>{{ key_illustration[props.row.illustration] }}</span>
                  </el-form-item>
                  <el-form-item label="类型1">
                    <span>{{ key_weapon_type1[props.row.weapon_type1] }}</span>
                  </el-form-item>
                  <el-form-item label="类型2">
                    <span>{{ key_weapon_type2[props.row.weapon_type2] }}</span>
                  </el-form-item>
                  <el-form-item label="负重">
                    <span>{{ props.row.weight }}</span>
                  </el-form-item>
                  <el-form-item label="亲密度">
                    <span>{{ props.row.intimacy }}</span>
                  </el-form-item>
                  <el-form-item label="攻击力">
                    <span>{{ props.row.attackValue }}</span>
                  </el-form-item>
                  <el-form-item label="射速">
                    <span>{{ props.row.handling }}</span>
                  </el-form-item>
                  <el-form-item label="载弹量">
                    <span>{{ props.row.bullets }}</span>
                  </el-form-item>
                  <el-form-item label="属性">
                    <div style="line-height: 100%;vertical-align: middle;padding:5px;">
                      <el-tooltip class="item" effect="dark" :content="props.row.attribute_info.name" placement="top-start">
                        <img :src="baseUrl+props.row.attribute_info.icon" style="width:25px;height:25px;">
                      </el-tooltip>
                    </div>
                  </el-form-item>
                  <el-form-item label="系列">
                    <div style="line-height: 100%;vertical-align: middle;padding:5px;">
                      <el-tooltip class="item" effect="dark" :content="props.row.series_info.name" placement="top-start">
                        <img :src="baseUrl+props.row.series_info.icon" style="width:25px;height:25px;">
                      </el-tooltip>
                    </div>
                  </el-form-item>
                </el-form>
              </template>
            </el-table-column>

            <el-table-column prop="name" label="名称" width="180" />
            <el-table-column prop="number" label="缩略图" width="220">
              <template slot-scope="scope">
                <el-button v-if="scope.row.thumbnails.length <=0" type="text" size="small" @click="updateWeaponImageClick(scope.row)">+设置图片</el-button>
                <img v-for="item in scope.row.thumbnails" :key="item.id" :src="baseUrl+item.url" style="width:50px;height:50px;">
              </template>
            </el-table-column>
            <el-table-column label="星级">
              <template slot-scope="scope">
                <el-rate v-model="scope.row.star" disabled :max="7" />
              </template>
            </el-table-column>
            <el-table-column label="是否觉醒">
              <template slot-scope="scope">
                {{ key_logic[scope.row.awakening] }}
              </template>
            </el-table-column>
            <el-table-column label="操作" width="180">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="deleteClick(scope.row)">删除</el-button>
                <el-button type="text" size="small" @click="editClick(scope.row)">修改</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-main>

      <el-footer>
        <div class="block">
          <el-pagination
            :current-page="page.current"
            :page-sizes="[5,10, 20, 50, 100]"
            :page-size="page.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-footer>

      <my-delete-dialog
        :show-dialog="dialogConfirmVisible"
        :row="currentWeaponTemp"
        @deleteTask="deleteTask"
        @cancelTask="dialogConfirmVisible = false"
      />

      <el-dialog :title="form.sourceChooseTitle" :visible.sync="dialogSourcesVisible" :close-on-click-modal="false" center append-to-body>
        <source-list @chooseSourceHandle="closeSourceDialog" />
      </el-dialog>
    </my-theme-card>
  </el-container>
</template>

<style>
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>

<script>
import request from '@/utils/request'
const sourceList = () => import('@/components/SourceList/index')
export default {
  components: {
    sourceList
  },
  data: function() {
    return {
      conditions: [
        {
          field: 'name',
          type: 'STRING',
          value: '',
          symbol: 'LIKE'
        }
      ],
      baseUrl: process.env.VUE_APP_BASE_API,
      form: {
        title: '添加徽章',
        sourceChooseTitle: '设置图片'
      },
      orders: [
        { field: 'creation_time', rule: 'desc' }
      ],
      page: {
        current: 1,
        size: 5,
        total: 0
      },
      weaponList: [],
      thumbnailsTemp: [],
      currentWeaponTemp: {},
      dialogSourcesVisible: false,
      dialogConfirmVisible: false
    }
  },
  computed: {
    key_illustration: function() {
      const { illustration = {}} = this.$store.state.directory.directoryItem
      return illustration
    },
    key_weapon_type1: function() {
      const { weapon_type1 = {}} = this.$store.state.directory.directoryItem
      return weapon_type1
    },
    key_weapon_type2: function() {
      const { weapon_type2 = {}} = this.$store.state.directory.directoryItem
      return weapon_type2
    },
    key_logic: function() {
      const { logic = {}} = this.$store.state.directory.directoryItem
      return logic
    }
  },
  created() {
    this.directoryUpdate()
    this.query()
  },
  methods: {
    directoryUpdate: function() {
      this.$store.commit('directory/setDirectory', 'illustration')
      this.$store.commit('directory/setDirectory', 'weapon_type1')
      this.$store.commit('directory/setDirectory', 'weapon_type2')
    },
    // 关闭
    closeSourceDialog: function(data) {
      if (data && data.length > 0) {
        this.thumbnailsTemp = []
        data.forEach((source) => {
          this.thumbnailsTemp.push({
            id: source
          })
        })
      }
      this.dialogSourcesVisible = false
      this.updateWeaponImageTask()
    },
    // 更新该装备缩略图关联
    updateWeaponImageTask: function() {
      this.$progress.start()
      request({
        url: '/bengbeng/weapon/updateWeaponThumbnails',
        method: 'post',
        data: {
          weapon: this.currentWeaponTemp,
          thumbnails: this.thumbnailsTemp
        }
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.query()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }
      ).catch(() => { this.$progress.done() })
    },
    // 设置装备图片点击事件
    updateWeaponImageClick: function(weapon) {
      this.currentWeaponTemp = weapon
      this.dialogSourcesVisible = true
    },
    // 添加徽章点击事件
    addClick: function() {
      this.$emit('toChangePage', 'add')
    },
    deleteClick: function(row) {
      this.currentWeaponTemp = row
      this.dialogConfirmVisible = true
    },
    deleteTask: function() {
      this.$progress.start()
      request({
        url: '/bengbeng/weapon/delete',
        method: 'post',
        data: this.currentWeaponTemp
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.dialogConfirmVisible = false
          this.query()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    },
    editClick: function(row) {
      this.$emit('toChangePage', 'edit', row)
    },
    // 每页显示数目发生变化时
    handleSizeChange: function(val) {
      this.page.size = val
      this.page.current = 1
      this.query()
    },
    // 当前页发生变化时
    handleCurrentChange: function(val) {
      this.page.current = val
      this.query()
    },
    // 搜索
    search: function() {
      this.page.current = 1
      this.query()
    },
    query: function() {
      this.$progress.start()
      this.page.conditions = this.conditions
      this.page.orders = this.orders
      request({
        url: '/bengbeng/weapon/query',
        method: 'post',
        data: this.page
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.page.total = res.data.total
          this.weaponList = res.data.data
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    }
  }
}
</script>
